Skip to content

fix: Bluetooth pads show the battery Windows shows - #225

Merged
lippdev merged 3 commits into
mainfrom
feat/bluetooth-battery
Nov 18, 2026
Merged

lippdev merged 3 commits into
mainfrom
feat/bluetooth-battery

Conversation

@lippdev

@lippdev lippdev commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Closes #224. First of three PRs for generic controllers in 1.7.1-beta.4 (then: Home button through standard HID usages, and a better controller test).

What

PLEXX-DE's Amazon Luna pad (Bluetooth LE, 0171:0419) showed 10 % in the corner while Settings → Bluetooth said 90 %. The log in the issue has bateria fraca (NDevice, 10%) on 9/11, 17/11 and 18/11: a fixed value from Windows.Gaming.Input (TryGetBatteryReport), not a draining battery.

For any Bluetooth game controller, Windows keeps the level it shows in Settings as a device property ({104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2, one byte) on the pad's Bluetooth device node.

  • Native/HidDevices (new): lists HID collections, queries one without read access (vendor, product, top-level usage), and walks the device tree (CM_Get_Parent, CM_Get_DevNode_PropertyW). Shared with the next PR.
  • BluetoothBatteryReader (new): from every gamepad/joystick collection (usage page 1, usage 4/5), walks up to 6 nodes looking for that property; one reading per Bluetooth device, named as Windows names it.
  • ControllerBattery.Combine (pure, tested) takes the Bluetooth readings: they replace Windows.Gaming.Input's reading of the same pad (same vendor and product id) and the rough XInput fallback. Sony HID still wins for PlayStation pads; pads on a cable or a dongle have no such node and are untouched.
  • BatteryReading gets optional VendorId/ProductId; the WGI readings fill them. The diagnostics report matches a Bluetooth reading to its pad by them.
  • The log names each Bluetooth pad once: Controle: bateria pelo Bluetooth do Windows: <name> (<vid:pid>) <n>%.

Changelogs (both) and docs/TESTING.md updated.

Testing

  • dotnet test: 700 passing (7 new). Debug build OK.
  • The owner's PC has no Bluetooth device. A small harness compiling HidDevices.cs listed the HID collections and walked their trees (HID → USB interface → receiver → hubs → root hub) with the right ids and names; the battery property read returned nothing on USB nodes, as expected. The debug app ran 45 s with no battery error in the log.
  • The Bluetooth value itself: to be confirmed by PLEXX-DE on the beta (checklist in docs/TESTING.md).

🤖 Generated with Claude Code

lippdev and others added 2 commits November 18, 2026 13:55
An Amazon Luna pad read 10 % through Windows.Gaming.Input for days while
Settings → Bluetooth said 90 %. Windows keeps the Bluetooth battery
level on the pad's Bluetooth device node; BluetoothBatteryReader walks
up from every gamepad HID collection to find it, and Combine lets that
reading replace Windows.Gaming.Input's (and the rough XInput one) for
the same pad, matched by vendor and product id. Sony HID still wins for
PlayStation pads.

Native/HidDevices holds the HID listing and device-tree queries, to be
shared with the generic Home-button reader.

Refs #224.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

The new Bluetooth battery path has a device-property lookup that can prevent readings from being found, and Bluetooth readings can hide unrelated XInput batteries.

Approach: sound — use Windows' Bluetooth battery value as the authoritative reading for a matching pad while preserving Sony HID readings.

Reviewed changes

  • Bluetooth device discovery: Enumerates HID controller collections and walks parent device nodes to find Windows' battery property.
  • Battery source integration: Adds Bluetooth readings to the existing Sony HID, WGI, and XInput selection and diagnostics paths.
  • Tests and documentation: Adds pure battery-selection tests, changelog entries, and manual Bluetooth checks.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using gpt-6-luna | 𝕏

public static uint DevNodeOf(string interfacePath)
{
var key = DevPKeyInstanceId;
var id = GetInterfaceString(interfacePath, ref key);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This asks the device-interface property API for DEVPKEY_Device_InstanceId, which is a device property rather than a device-interface property. If Windows does not expose that key on the interface, this returns no ID, DevNodeOf returns 0, and the battery reader never walks the device tree, so no Bluetooth readings are produced.

Technical details
# Resolve the HID interface to its device node

## Affected sites
- `src/ConsoleMode/Native/HidDevices.cs:76` — requests `DEVPKEY_Device_InstanceId` via `CM_Get_Device_Interface_PropertyW`.
- `src/ConsoleMode/Native/HidDevices.cs:138` — returns `0` when this lookup fails.
- `src/ConsoleMode/Services/BluetoothBatteryReader.cs:29-30` — skips the property walk for node `0`.

## Required outcome
- Reliably resolve each HID interface to its associated device node so the Bluetooth battery property can be read.

## Suggested approach
- Use a device-interface property to obtain the associated device instance ID, or retrieve the device node through the SetupAPI interface detail call.

## Evidence
- [DEVPKEY_Device_InstanceId](https://learn.microsoft.com/en-us/windows-hardware/drivers/install/devpkey-device-instanceid)
- [CM_Get_Device_Interface_PropertyW](https://learn.microsoft.com/en-us/windows/win32/api/cfgmgr32/nf-cfgmgr32-cm_get_device_interface_propertyw)
- [SetupDiGetDeviceInterfaceDetailW](https://learn.microsoft.com/en-us/windows/win32/api/setupapi/nf-setupapi-setupdigetdeviceinterfacedetailw)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows does expose DEVPKEY_Device_InstanceId on device interfaces (it is in the documented set of device interface properties). Checked on the owner's PC with a harness compiling this file: DevNodeOf resolved every HID interface there (Logitech receiver, Apple keyboard, other HID devices) and the walk went HID → USB interface → receiver → hubs → root hub with the right instance ids. Leaving it as is.

result.AddRange(fromBluetooth);
result.AddRange(fromWgi.Select(w => w.Reading));
if (!fromWgi.Any(w => !w.Sony)) result.AddRange(xinput);
if (fromBluetooth.Count == 0 && !fromWgi.Any(w => !w.Sony)) result.AddRange(xinput);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gates the entire XInput fallback on whether any Bluetooth pad was found. With one Bluetooth reading, an unrelated XInput-only controller whose battery is not reported by WGI is omitted from the combined list. Keep the fallback available for other pads while avoiding a duplicate reading for the Bluetooth pad.

Technical details
# Preserve unrelated XInput fallback readings

## Affected sites
- `src/ConsoleMode/Services/ControllerBattery.cs:137` — suppresses all XInput readings when `fromBluetooth` is non-empty.

## Required outcome
- A Bluetooth battery reading should replace or suppress only a reading for that same pad; it should not hide an unrelated XInput-only pad.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intended, and the same trade-off the existing rule makes for Windows.Gaming.Input: XInput slots can't be tied to a device (no vendor/product id), so with a Bluetooth pad present its own XInput reading would show as a second, rough battery for the same pad. The case left out is a wireless XInput-only pad whose battery Windows.Gaming.Input doesn't report, next to a Bluetooth pad; rare, and it only hides the XInput estimate. Leaving it as is.

# Conflicts:
#	CHANGELOG.en-US.md
#	CHANGELOG.md
@lippdev
lippdev merged commit 3fc0ed8 into main Nov 18, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Battery indicator shows wrong value

1 participant